package com.work.csms.mapper;

import com.work.csms.dao.Money;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MoneyMapper {

    @Select({"<script>" +
            "SELECT m.*,u.username FROM money m left join user u on m.userid=u.userid " +
            " WHERE 1=1 " +
            "<if test=\"userid != null \"> " +
            " and m.userid = #{userid}" +
            "</if>" +
            "<if test=\"orderid != null \"> " +
            " and m.orderid = #{orderid}" +
            "</if>" +
            "<if test=\"type != null and type != '' \"> " +
            " and m.type = #{type}" +
            "</if>" +
            " order by date desc" +
            "</script>"})
    List<Money> list(Money money);

    @Select("SELECT * FROM money WHERE id=#{id}")
    Money getMoneyById(Integer id);


    @Update({"<script>" +
            "update money set " +
            "<if test=\"userid != null \"> " +
            " userid = #{userid}," +
            "</if>" +
            "<if test=\"orderid != null \"> " +
            " orderid = #{orderid}," +
            "</if>" +
            "<if test=\"money != null \"> " +
            " money = #{money}," +
            "</if>" +
            "<if test=\"type != null and type != '' \"> " +
            " type = #{type}," +
            "</if>" +
            "<if test=\"date != null \"> " +
            " date = #{date}," +
            "</if>" +
            "<if test=\"remark != null and remark != '' \"> " +
            " remark = #{remark}," +
            "</if>" +
            " id = #{id}" +
            " where id = #{id}" +
            "</script>"})
    void update(Money money);

    @Insert("INSERT INTO money(userid, orderid, money, type, date, remark) VALUES " +
            "(#{userid}, #{orderid}, #{money}, #{type}, #{date}, #{remark})")
    void save(Money money);

    @Delete("delete from money where id = #{id} ")
    void deleted(Integer id);
}
